package org.jz.gae.redbook.spb.server.controller;

import java.util.List;

import org.jz.gae.redbook.spb.server.cache.FamilyCashe;
import org.jz.gae.redbook.spb.server.helper.RedBookHelper;
import org.jz.gae.redbook.spb.server.helper.SpeciesHelper;
import org.jz.gae.redbook.spb.shared.FamilyEntityView;
import org.jz.gae.redbook.spb.shared.SpeciesEntityView;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class SpeciesControllerGoogle implements SpeciesController
{

	@Override
	public void loadSpeciesList(
			List<SpeciesEntityView> _SpeciesList)
	{
	    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Key redbookKey = KeyFactory.createKey(RedBookHelper.RED_BOOK_SIGN, RedBookHelper.RED_BOOK_NAME);
	    Query query = new Query(SpeciesHelper.ENTITY_NAME, redbookKey).addSort(SpeciesHelper.SPECIES_LAT_NAME, Query.SortDirection.ASCENDING);
	    List<Entity> speciess = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
	    if (speciess.isEmpty()) 
	    {
	    	_SpeciesList.clear();
	    } 
	    else 
	    {
	    	_SpeciesList.clear();
    		FamilyCashe familyCashe = FamilyCasheGoogle.getInstance();
	    	for (Entity species : speciess)
	    	{
	    		SpeciesEntityView newSpecies = new SpeciesEntityView();
	    		SpeciesUtils.reflectEntityToView(species, newSpecies);
	    		newSpecies.setSpeciesFamilyPresentation(familyCashe.getEntityView(newSpecies.getSpeciesFamilyKey()).getFamilyLatName());
	    		/**/
	    		_SpeciesList.add(newSpecies);
	    	}
	    }		
	}
	

	@Override
	public SpeciesEntityView loadSpecies(String _SpeciesId) throws Exception
	{
		System.err.println("loading key : " + _SpeciesId);
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity species = datastore.get(KeyFactory.stringToKey(_SpeciesId));
		System.err.println("loaded");
		SpeciesEntityView result = new SpeciesEntityView();
		SpeciesUtils.reflectEntityToView(species, result);
		FamilyCashe familyCashe = FamilyCasheGoogle.getInstance();
		result.setSpeciesFamilyPresentation(familyCashe.getEntityView(result.getSpeciesFamilyKey()).getFamilyLatName());
		return result;
	}
	
	@Override
	public void updateSpecies(
			SpeciesEntityView _SpeciesToEdit) throws Exception
	{
		System.err.println("extraction key : " + _SpeciesToEdit.getSpeciesKey());
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity species = datastore.get(KeyFactory.stringToKey(_SpeciesToEdit.getSpeciesKey()));
		System.err.println("extracted");
		SpeciesUtils.reflectViewToEntity(_SpeciesToEdit, species);
        datastore.put(species);
		System.err.println("updated");
	}

	@Override
	public void deleteSpecies(
			SpeciesEntityView _SpeciesToDelete)
	{
		System.err.println("deletion key : " + _SpeciesToDelete.getSpeciesKey());
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.delete(KeyFactory.stringToKey(_SpeciesToDelete.getSpeciesKey()));
		System.err.println("deleted");
	}

	@Override
	public void createSpecies(
			SpeciesEntityView _SpeciesToAppend)
	{
	    Key redbookKey = KeyFactory.createKey(RedBookHelper.RED_BOOK_SIGN, RedBookHelper.RED_BOOK_NAME);
		Entity newSpecies = new Entity(SpeciesHelper.ENTITY_NAME, redbookKey);
		SpeciesUtils.reflectViewToEntity(_SpeciesToAppend, newSpecies);
        /**/
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(newSpecies);
		System.err.println("new key : " + newSpecies.getKey());
		_SpeciesToAppend.setSpeciesKey(KeyFactory.keyToString(newSpecies.getKey()));
	}

}
